import math
import random
import pandas as pd
import matplotlib.pyplot as plt
import requests
import json
import folium
from pso4cvrp import PSO
from function import *

if __name__ == '__main__':
    # 读入数据
    customer_list = [{'customer_id': 0, 'coordinate': (116.378875,39.830385), 'demand': 0, 'name': '00北京货运中心'},
                     {'customer_id': 1, 'coordinate': (116.336304,39.897873), 'demand': 106, 'name': '01物美超市(华天大厦店)'},
                     {'customer_id': 2, 'coordinate': (116.209734,39.845445), 'demand': 101, 'name': '02物美超市(顶层家居智媒广场店)'},
                     {'customer_id': 3, 'coordinate': (116.375152,39.893375), 'demand': 60, 'name': '03沃尔玛(富卓商厦店)'},
                     {'customer_id': 4, 'coordinate': (116.419116,39.896463), 'demand': 100, 'name': '04永辉超市(Bravo搜秀城店)'},
                     {'customer_id': 5, 'coordinate': (116.379884,39.925904), 'demand': 70, 'name': '05顺天府超市(府右街店)'},
                     {'customer_id': 6, 'coordinate': (116.272453,39.888329), 'demand': 102, 'name': '06物美超市(大成路店)'},
                     {'customer_id': 7, 'coordinate': (116.294458,39.864404), 'demand': 16, 'name': '07物美超市(东大街店)'},
                     {'customer_id': 8, 'coordinate': (116.317208,39.879463), 'demand': 60, 'name': '08永辉超市(丰台区六里桥店)'},
                     {'customer_id': 9, 'coordinate': (116.240395,39.834702), 'demand': 16, 'name': '09物美超市(晓月苑店)'},
                     {'customer_id': 10, 'coordinate': (116.317721,39.981), 'demand': 80, 'name': '10超市发(科学城店)'},
                     {'customer_id': 11, 'coordinate': (116.231937,39.894154), 'demand': 140, 'name': '11永辉超市(鲁谷店)'},
                     {'customer_id': 12, 'coordinate': (116.460889,39.978775), 'demand': 70, 'name': '12家乐福(四元桥店)'},
                     {'customer_id': 13, 'coordinate': (116.53136,39.969355), 'demand': 16, 'name': '13物美大卖场(东坝恒大店)'},
                     {'customer_id': 14, 'coordinate': (116.254035,39.93097), 'demand': 30, 'name': '14物美超市(田村店)'},
                     {'customer_id': 15, 'coordinate': (116.511408,39.863389), 'demand': 22, 'name': '15京客隆超市(垡头店)'},
                     {'customer_id': 16, 'coordinate': (116.473823,39.858004), 'demand': 180, 'name': '16麦德龙(北京十里河商场)'},
                     {'customer_id': 17, 'coordinate': (116.49825,39.791389), 'demand': 19, 'name': '17美廉美超市(亦庄店)'},
                     {'customer_id': 18, 'coordinate': (116.574562,39.854007), 'demand': 15, 'name': '18多达百货超市'},
                     {'customer_id': 19, 'coordinate': (116.579567,39.817757), 'demand': 14, 'name': '19永辉超市(通州区台湖泰禾店)'},
                     {'customer_id': 20, 'coordinate': (116.293588,39.844546), 'demand': 80, 'name': '20物美超市(丰台新华东店)'},
                     {'customer_id': 21, 'coordinate': (116.509337,40.023474), 'demand': 12, 'name': '21马南里生活超市(马南里便利店)'},
                     {'customer_id': 22, 'coordinate': (116.292071,39.817966), 'demand': 4, 'name': '22永旺超市(永旺梦乐城店)'},
                     {'customer_id': 23, 'coordinate': (116.358115,39.85116), 'demand': 80, 'name': '23麦德龙会员店(草桥上品+店)'},
                     {'customer_id': 24, 'coordinate': (116.299478,39.824816), 'demand': 24, 'name': '24永辉超市(丰科万达店)'},
                     {'customer_id': 25, 'coordinate': (116.44808,39.75855), 'demand': 24, 'name': '25永辉超市(瀛海环宇坊店)'},
                     {'customer_id': 26, 'coordinate': (116.424993,39.798776), 'demand': 32, 'name': '26世纪华联超市(万源里店)'},
                     {'customer_id': 27, 'coordinate': (116.479525,39.795687), 'demand': 10, 'name': '27京客隆超市(亦庄店)'},
                     {'customer_id': 28, 'coordinate': (116.365285,39.793909), 'demand': 15, 'name': '28京客隆(西红门店)'},
                     {'customer_id': 29, 'coordinate': (116.408893,39.985067), 'demand': 11, 'name': '29华联超市(安苑北里店)'},
                     {'customer_id': 30, 'coordinate': (116.4312,39.986597), 'demand': 14, 'name': '30天天如壹生活超市(育慧南路店)'},
                     {'customer_id': 31, 'coordinate': (116.374131,40.001284), 'demand': 21, 'name': '31物美超市(北沙滩店)'},
                     {'customer_id': 32, 'coordinate': (116.339571,39.791708), 'demand': 50, 'name': '32永辉超市(大兴区鸿坤理想城店)'}]

    # 经纬度
    Customer = [gcj02_to_mercator(i['coordinate'][0], i['coordinate'][1]) for i in customer_list]
    # 需求
    Demand = [i['demand'] for i in customer_list]
    dis_matrix = calDistance(Customer)  # 计算配送点间的距离矩阵

    pso = PSO()
    gLine_car = pso.iteration(Customer, dis_matrix, Demand)

    draw_path(gLine_car, Customer)  # 画路径图
    draw_gps(customer_list, gLine_car)
    # route_info = draw_gps(customer_list, gLine_car)
    # for i in range(len(route_info)):
    #     cap = 0
    #     for n in range(len(route_info[i][2])):
    #         cap += Demand[route_info[i][2][n]]
    #     print('第{}辆车依次经过的站点编号为{}，总容量{}，总路程{}公里,总费用{}元' \
    #           .format(i + 1,
    #                   route_info[i][2],
    #                   cap,
    #                   round(float(route_info[i][0]) / 1000, 1),
    #                   route_info[i][1]))